package ar.com.mooral.activities;



import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.ImageButton;
import ar.com.mooral.crohn.application.CrohnContext;
import ar.com.mooral.crohn.view.CrohnButton;
import ar.com.mooral.data.Result;


public class MiEstado extends CrohnActivity{
	
	private Button btnSemana;
	private Button btnMes;
	private Button btn3Mes;
	private Button btn6Mes;
	private Button btnRango;

	
	protected void onStart() {
		super.onStart();


		// Default to week
    	resetBg();
    	
    	if (CrohnContext.getRecordsMinRange() == null && CrohnContext.getRecordsMaxRange() == null) {
    		
        	((Button) findViewById(R.id.btnRangoSemana)).setBackgroundResource(R.drawable.boton_seleccionado_chico);
        	
	    	Calendar c = Calendar.getInstance();
	    	c.add(Calendar.DATE, -7);
	    	SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
	    	
	    	CrohnContext.setPeriodo("1");
	    	CrohnContext.setRecordsMinRange(formatter.format((Date) c.getTime()).toString());
	    	CrohnContext.setRecordsMaxRange(CrohnContext.getDateTimeNow());
    	} else {
    		if (CrohnContext.getPeriodo().equals("1")) {
    			((Button) findViewById(R.id.btnRangoSemana)).setBackgroundResource(R.drawable.boton_seleccionado_chico);
    		} else if (CrohnContext.getPeriodo().equals("2")) {
    			((Button) findViewById(R.id.btnRangoMes)).setBackgroundResource(R.drawable.boton_seleccionado_chico);
    		} else if (CrohnContext.getPeriodo().equals("3")) {
    			((Button) findViewById(R.id.btnRango3Meses)).setBackgroundResource(R.drawable.boton_seleccionado_chico);
    		} else if (CrohnContext.getPeriodo().equals("4")) {
    			((Button) findViewById(R.id.btnRango6Meses)).setBackgroundResource(R.drawable.boton_seleccionado_chico);
    		} else if (CrohnContext.getPeriodo().equals("5")) {
    			((Button) findViewById(R.id.btnRango)).setBackgroundResource(R.drawable.boton_seleccionado_chico);
    		}
    	}
		
    }	
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.mi_estado);

        
		// SEMANA
        btnSemana = (Button) findViewById(R.id.btnRangoSemana);
        btnSemana.setOnClickListener (new View.OnClickListener() {
		    public void onClick(View view) {
		    	
		    	resetBg();
		    	((Button) view).setBackgroundResource(R.drawable.boton_seleccionado_chico);
		    	
		    	Calendar c = Calendar.getInstance();
		    	c.add(Calendar.DATE, -7);
		    	SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
		    	
		    	CrohnContext.setPeriodo("1");
		    	CrohnContext.setRecordsMinRange(CrohnContext.getDateTimeNow());
		    	CrohnContext.setRecordsMaxRange(formatter.format((Date) c.getTime()).toString());
		    	
				//Intent intent = new Intent();
		    	//intent.setComponent(new ComponentName(Registros.class.getPackage().getName(), Registros.class.getName()));
		    	//startActivity(intent);
		    }
		});
        
        
		// MES
        btnMes = (Button) findViewById(R.id.btnRangoMes);
        btnMes.setOnClickListener (new View.OnClickListener() {
		    public void onClick(View view) {

		    	resetBg();
		    	((Button) view).setBackgroundResource(R.drawable.boton_seleccionado_chico);

		    	Calendar c = Calendar.getInstance();
		    	c.add(Calendar.MONTH, -1);
		    	SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
		    	
		    	CrohnContext.setPeriodo("2");
		    	CrohnContext.setRecordsMinRange(formatter.format((Date) c.getTime()).toString());
		    	CrohnContext.setRecordsMaxRange(CrohnContext.getDateTimeNow());
		    	
		    }
		});
        
        
		// 3 MESES
        btn3Mes = (Button) findViewById(R.id.btnRango3Meses);
        btn3Mes.setOnClickListener (new View.OnClickListener() {
		    public void onClick(View view) {
		
		    	resetBg();
		    	((Button) view).setBackgroundResource(R.drawable.boton_seleccionado_chico);
		    	
		    	
		    	Calendar c = Calendar.getInstance();
		    	c.add(Calendar.MONTH, -3);
		    	SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
		    	
		    	CrohnContext.setPeriodo("3");
		    	CrohnContext.setRecordsMinRange(formatter.format((Date) c.getTime()).toString());
		    	CrohnContext.setRecordsMaxRange(CrohnContext.getDateTimeNow());
		    	
		    }
		});		
        
        
        
		// 6 MESES
        btn6Mes = (Button) findViewById(R.id.btnRango6Meses);
        btn6Mes.setOnClickListener (new View.OnClickListener() {
		    public void onClick(View view) {
		    	
		    	resetBg();
		    	((Button) view).setBackgroundResource(R.drawable.boton_seleccionado_chico);

		    	
		    	Calendar c = Calendar.getInstance();
		    	c.add(Calendar.MONTH, -6);
		    	SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
		    
		    	CrohnContext.setPeriodo("4");
		    	CrohnContext.setRecordsMinRange(formatter.format((Date) c.getTime()).toString());
		    	CrohnContext.setRecordsMaxRange(CrohnContext.getDateTimeNow());
		    	
		    }
		});
        
        
        
		// RANGO
        btnRango = (Button) findViewById(R.id.btnRango);
        btnRango.setOnClickListener (new View.OnClickListener() {
		    public void onClick(View view) {
		    
		    	resetBg();
		    	((Button) view).setBackgroundResource(R.drawable.boton_seleccionado_chico);
		    	CrohnContext.setPeriodo("5");

		    	
				Intent intent = new Intent();
				intent.setComponent(new ComponentName(SeleccionRangos.class.getPackage().getName(), SeleccionRangos.class.getName()));
				startActivity(intent);
				
         		//TabGroupActivity parentActivity = (TabGroupActivity)getParent();
         		//parentActivity.startChildActivity("SeleccionRangos", intent);
				
		    }
		});
        
        
        
		// MI ESTADO INFORME
        Button btnMiEstadoVerInforme = (Button) findViewById(R.id.btnMiEstadoVerInforme);
        btnMiEstadoVerInforme.setOnClickListener (new View.OnClickListener() {
		    public void onClick(View view) {
		    	
		    	if (CrohnContext.getRecordsMinRange() == null || CrohnContext.getRecordsMaxRange() == null) {
		    		Result r = new Result(Result.CRITICAL, "Debe seleccionar el rango antes de continuar.");
		    		showMessage(r);
		    	} else {
		    	
					Intent intent = new Intent();
					intent.setComponent(new ComponentName(Registros.class.getPackage().getName(), Registros.class.getName()));
					//startActivity(intent);
	         		TabGroupActivity parentActivity = (TabGroupActivity)getParent();
	         		parentActivity.startChildActivity("Registros", intent);
					
		    		
		    	}
		    	
		    }
		});		


		// GRAFICA
        Button btnMiEstadoGrafica = (Button) findViewById(R.id.btnMiEstadoGrafica);
        btnMiEstadoGrafica.setOnClickListener (new View.OnClickListener() {
		    public void onClick(View view) {
		    	
		    	if (CrohnContext.getRecordsMinRange() == null || CrohnContext.getRecordsMaxRange() == null) {
		    		Result r = new Result(Result.CRITICAL, "Debe seleccionar el rango antes de continuar.");
		    		showMessage(r);
		    	} else {
		    	
					Intent intent = new Intent();
					intent.setComponent(new ComponentName(Grafica.class.getPackage().getName(), Grafica.class.getName()));
					startActivity(intent);

		    	}
		    	
		    }
		});		

    }
    
    
    private void resetBg() {
    	btnSemana.setBackgroundResource(R.drawable.boton_chico);
    	btnMes.setBackgroundResource(R.drawable.boton_chico);
    	btn3Mes.setBackgroundResource(R.drawable.boton_chico);
    	btn6Mes.setBackgroundResource(R.drawable.boton_chico);
    	btnRango.setBackgroundResource(R.drawable.boton_chico);
    } 
    
}